package com.chwn.curiousdiary.utils;

import android.content.Context;
import android.os.Environment;

import com.chwn.curiousdiary.DiaryApplication;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by Myshikin on 2017/4/7.
 */
public class FileUtils {

    /**
     * 获取缓存文件目录
     * @param context
     * @param uniqueName
     * @return
     */
    private static File getDiskCacheDir(Context context, String uniqueName) {
        String cachePath;

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            //如果SD卡存在通过getExternalCacheDir()获取路径，
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            //如果SD卡不存在通过getCacheDir()获取路径，
            cachePath = context.getCacheDir().getPath();
        }
        //放在路径 /.../data/<application package>/cache/uniqueName
        return new File(cachePath + File.separator + uniqueName);
    }

    private static final String SD_PATH = Environment.getExternalStorageDirectory().getPath();

    public static final String NAME = "GSYVideo";

    public static final String NAME_TEST = "GSYVideoTest";


    public static String getAppPath(String name) {
        StringBuilder sb = new StringBuilder();
        sb.append(SD_PATH);
        sb.append(File.separator);
        sb.append(name);
        sb.append(File.separator);
        return sb.toString();
    }

    public static String getPath() {
        return getAppPath(NAME);
    }

    public static String getTestPath() {
        return getAppPath(NAME_TEST);
    }

    public static void deleteFiles(File root) {
        File files[] = root.listFiles();
        if (files != null) {
            for (File f : files) {
                if (!f.isDirectory() && f.exists()) { // 判断是否存在
                    try {
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static boolean deleteFile(File f) {
        if(f.isDirectory()) {
            File[] files = f.listFiles();
            if(files != null && files.length > 0) {
                for(int i = 0; i < files.length; ++i) {
                    deleteFile(files[i]);
                }
            }
        }

        return f.delete();
    }


    public static String getCacheFileSize() {
        File responses = StorageUtils.getCacheDirectory(DiaryApplication.mInstance, true);
        long length = responses.length();
        return getDirSizeStr(responses);
    }

    public static double getDirSize(File file) {
        if (file.exists()) {
            if (!file.isDirectory()) {
                double var8 = (double) file.length() / 1024.0D / 1024.0D;
                return var8;
            } else {
                File[] size = file.listFiles();
                double size1 = 0.0D;
                if (size != null) {
                    File[] var7 = size;
                    int var6 = size.length;

                    for (int var5 = 0; var5 < var6; ++var5) {
                        File f = var7[var5];
                        size1 += getDirSize(f);
                    }
                }

                return size1;
            }
        } else {
            return 0.0D;
        }
    }

    public static String getDirSizeStr(File file) {
        DecimalFormat df = new DecimalFormat("#0.0");
        String size = df.format(getDirSize(file));
        return size + "M";
    }

    public static boolean clearAppCache() {
        File responses = StorageUtils.getCacheDirectory(DiaryApplication.mInstance, true);
        boolean b = deleteFile(responses);
        return b ;
    }
}
